package io.renren.modules.work.service.impl;

import io.renren.modules.work.entity.ScExpenseEntity;
import io.renren.modules.work.service.ScExpenseService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.work.dao.ScExpensedtlDao;
import io.renren.modules.work.entity.ScExpensedtlEntity;
import io.renren.modules.work.service.ScExpensedtlService;


@Service("scExpensedtlService")
public class ScExpensedtlServiceImpl extends ServiceImpl<ScExpensedtlDao, ScExpensedtlEntity> implements ScExpensedtlService {

    @Autowired
    private ScExpenseService scExpenseService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<ScExpensedtlEntity> scExpensedtlEntityQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank((String) params.get("expenseid"))){
            scExpensedtlEntityQueryWrapper.eq("expendseid",params.get("expenseid"));
        }
        IPage<ScExpensedtlEntity> page = this.page(
                new Query<ScExpensedtlEntity>().getPage(params),
                scExpensedtlEntityQueryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void caculate(String expenseid) {
        QueryWrapper<ScExpensedtlEntity> scExpensedtlEntityQueryWrapper = new QueryWrapper<>();
        scExpensedtlEntityQueryWrapper.eq("expendseid",expenseid);
        List<ScExpensedtlEntity> scExpensedtlEntities = baseMapper.selectList(scExpensedtlEntityQueryWrapper);
        BigDecimal total = new BigDecimal(0);
        for (ScExpensedtlEntity scExpensedtlEntity : scExpensedtlEntities) {
            total = total.add(scExpensedtlEntity.getPrice());
        }
        ScExpenseEntity scExpenseEntity = scExpenseService.getById(expenseid);
        scExpenseEntity.setPrice(total);
        scExpenseService.updateById(scExpenseEntity);
    }

}